/*
 * Copyright (c) 2016- 2020 qolome.com Inc. All Rights Reserved.
 * ProjectName: qolome-knowledge
 * FileName: BeanUtils.java
 * Author: jiangfeng@qolome.com
 * Date: 2020/3/1 下午3:26
 * LastModified: 2020/03/01 15:26:01
 */

package com.qolome.knowledge.design.pattern.prototype;

import java.lang.reflect.Field;

/**
 * @author Qolome
 * @version V1.0
 * @Description (原型模式)浅克隆
 * @date 2020年 03月01日 15:26:47
 */
public class BeanUtils {

    public static  Object copy(Object prototype){

        Class clazz = prototype.getClass();
        Object returnValue = null;
        try {
            returnValue = clazz.newInstance();

            for (Field field:clazz.getDeclaredFields()) {
                field.setAccessible(true);
                field.set(returnValue, field.get(prototype));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnValue;
    }
}
